//
//  unLoginView.swift
//  MyWeibo
//
//  Created by stone on 15/7/29.
//  Copyright © 2015年 gff. All rights reserved.
//

import UIKit

class unLoginView: UIView {

    //MARK: - 设置图标
    func setIconImage(isHome:Bool, iconName:String,labText:String) {
        iconImage.image = UIImage(named: iconName)
        textLabel.text = labText
        homeImage.hidden = !isHome
        isHome ? iconImageAnimation() :(sendSubviewToBack(coverImage))
    }
   override init(frame: CGRect) {
    super.init(frame: frame)
    backgroundColor = UIColor(white: 237/255.0, alpha: 1)
    addSubviewsUnlogin()
    setFrames()
    }

   required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
   }
    //MARK: - 添加子控件
    private func addSubviewsUnlogin()
    {
        addSubview(iconImage)
        addSubview(coverImage)
        addSubview(homeImage)
        addSubview(textLabel)
        addSubview(registButton)
        addSubview(loginButton)
    }
    private func setFrames()
    {
        iconImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        coverImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskView]-0-|", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["maskView":coverImage]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskView]-(-35)-[registButton]", options: NSLayoutFormatOptions(rawValue:0), metrics: nil, views: ["maskView":coverImage,"registButton":registButton]))
        
        homeImage.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeImage, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeImage, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconImage, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
         addConstraint(NSLayoutConstraint(item: textLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        registButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
         addConstraint(NSLayoutConstraint(item: registButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: textLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
         addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
         addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
    }
    //MARK: - iconImage动画
    private func iconImageAnimation() {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.toValue = 2 * M_PI
        ani.duration = 20.0
        ani.repeatCount = MAXFLOAT
        ani.removedOnCompletion = false
        iconImage.layer.addAnimation(ani, forKey: nil)
    }
    //MARK: - 懒加载控件
    /// 最底部图片
    private lazy var iconImage:UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return img
    }()
    ///中间覆盖层
    private lazy var coverImage:UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return img
    }()
    ///   最上层图片
    private lazy var homeImage:UIImageView = {
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return img
    }()
    ///   文字信息
    private lazy var textLabel:UILabel = {
        let lab = UILabel()
        lab.text = "关注一些人，回这里看看有什么惊喜"
        lab.textAlignment = NSTextAlignment.Center
        lab.font = UIFont.systemFontOfSize(14)
        lab.textColor = UIColor.darkGrayColor()
        lab.numberOfLines = 0
        lab.sizeToFit()
        return lab
    }()
    ///   注册按钮
    private lazy var registButton:UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return btn
    }()
    /// 登录按钮
    private lazy var loginButton:UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        return btn
    }()
}
